import { defineStore } from "pinia"
import { Params, Statistics } from '@/types/home'
import { IdLabel,IdName } from '@/types/global'
import { getStatistics, getCommunityList, getCommunityChildList, getEnvelopmentInfo } from '@/api/home'
import { useUserStore } from "./user";

export const useHomeStore = defineStore({
  id: 'home',
  state() {
    return {
      statistics: {
        allConfirmCount: 0,  //全部人员数量
        zdCount: 0,          //重点人员数量
        wlCount: 0,          //外来人员数量
        tsCount: 0,          //特殊人群数量
        unConfirmCount: 0,   //尚未确认管控人员数量
        IsolateCount: 0,     //隔离人员数量
        unIsolateCount: 0,   //解除隔离人数
        todayFreeCount: 0,   //今日解封人数
        tomorrowFreeCount: 0 //明日解封人数
      } as Statistics
    };
  },
  actions: {
    getStatistics(payload: Params) {
      return new Promise((resolve, reject) => {
        getStatistics(payload)
          .then(res => {
            const data = res.data as Statistics[]
            if(res.code === 200) {
              this.statistics = data[0]
            }
            resolve(data)
          })
          .catch(err => {
            reject(err)
          })
      })
    },
    getCommunityList(): Promise<IdName[]> {
      return new Promise((resolve, reject) => {
        getCommunityList()
          .then(res => {
            const data = res.data as IdName[]
            resolve(data)
          })
          .catch(err => {
            reject(err)
          })
      })
    },
    getCommunityChildListById(payload: string): Promise<IdName[]> {
      return new Promise((resolve, reject) => {
        getCommunityChildList(payload)
          .then(res => {
            const data = res.data as IdName[]
            resolve(data)
          })
          .catch(err => {
            reject(err)
          })
      })
    },
    getEnveLopInfo(payload: string): Promise<any>{
      return new Promise((resolve, reject) => {
        getEnvelopmentInfo(payload)
          .then(res => {
            const data = res.data as any
            resolve(data)
          })
          .catch(err => {
            reject(err)
          })
      })
    }
  },
  getters: {
    communityList(): IdLabel[] {
      const { userInfo: { community_arr } } = useUserStore()
      return community_arr
    },
    communityALlChild() {
      const { userInfo: { community_child_arr } } = useUserStore()
      return community_child_arr
    }
  }
})

